/**
 * @Author : woon
 * @Timestamp : 2016-06-29
 */
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var circleUrl = require("url/circle");
var http = require("helper/http");
var open = require("helper/open");
var common = require("common");
var userUtil = require("helper/user");
var circleUtil = require("helper/circle");
var imHelper = require("helper/im");

var rootview = ui("$"); 
var el=ui("expandableListView1");

//声明UI变量
var listdata4G = mm("do_ListData");//分组数据
var listdata4Member = mm("do_ListData");//成员数据


var httpUrl4CircleMembers = circleUrl.url.member.members4circle;

var circleId = null;

//andriod返回键
page.on("back", function() {
    app.closePage();
});

page.on("result", function(data){
	if (data.memberAdd || data.memberDel) {
		bindMemberData();
    }
});


/****************处理页面-头部-begin*****************/
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);

//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
	app.closePage();
});

var header = ui("header");
header.bindData(hashdata);
hashdata.addData({"labelTitle" : "群聊成员"});
header.refreshData();

//右上角-
var headerRight = ui(addheader + ".headerRight");
// 右上角添加+图标
var imgNext = ui(addheader + ".imgNext");
imgNext.source = "source://image/common/more.png";
imgNext.visible = true;

var menus=[{"template" : 0,"menuIcon":"source://image/menu/useradd.png","menuName" : "邀请新成员","type":"addMember"}];
var popmenu = rootview.add("menupop", "source://view/reuse/menu/menu.ui", 0, 0);
var popmenuf = ui("menupop");
headerRight.on("touch", function() {
	popmenuf.fire("popmenuSJ", menus);
});
/****************处理页面-头部-end*****************/

var canInviteUser = false;
var isGroupCreator = false;
var isGroupManager = false;
var myNick = null;
var imGroupId = null;
page.on("loaded", function(){
	circleId = page.getData().circleId;//读取当前页面的传入参数
	getMyInfo();
	getGroupInfo();
	bindMemberData();
});

function getGroupInfo() {
	imHelper.loadGroupByGroupId(circleId, function(imGroup) {
		imGroupId = imGroup.imId;
	})
}
function getMyInfo() {
	imHelper.loadMe(function(myImUser) {
		myNick = myImUser.name;
	})
}
//全部刷新数据
function bindMemberData(){
	var managerList = [];//管理员(含群主)
	var memberList = [];//普通成员
	listdata4G.removeAll();
	listdata4Member.removeAll();
	http.post(httpUrl4CircleMembers,{circleId:circleId},function(data){
		var members = data.members;
		canInviteUser = data.canInviteUser;//是否可以邀请新成员
		isGroupCreator = data.isGroupCreator;//是否群主
		isGroupManager = data.isGroupManager;//是否管理员
		if(isGroupCreator){
			menus =[{"template" : 0,"menuIcon":"source://image/menu/useradd.png","menuName" : "邀请新成员","type":"addMember"},
			        {"template" : 0,"menuIcon":"source://image/menu/del.png","menuName" : "解散群聊","type":"dismissGroup"},
			        {"template" : 0,"menuIcon":"source://image/setting/setting.png","menuName" : "管理成员","type":"manageMember"},
			        {"template" : 0,"menuIcon":"source://image/setting/setting.png","menuName" : "设置","type":"setGroup"}];
		}else if(isGroupManager){
			menus =[{"template" : 0,"menuIcon":"source://image/menu/useradd.png","menuName" : "邀请新成员","type":"addMember"},
			        {"template" : 0,"menuIcon":"source://image/common/quit.png","menuName" : "退出群聊","type":"quitGroup"},
			        {"template" : 0,"menuIcon":"source://image/setting/setting.png","menuName" : "管理成员","type":"manageMember"},
			        {"template" : 0,"menuIcon":"source://image/setting/setting.png","menuName" : "设置","type":"setGroup"}];
		}else if(canInviteUser){
			menus =[{"template" : 0,"menuIcon":"source://image/menu/useradd.png","menuName" : "邀请新成员","type":"addMember"},
			        {"template" : 0,"menuIcon":"source://image/common/quit.png","menuName" : "退出群聊","type":"quitGroup"},
			        {"template" : 0,"menuIcon":"source://image/setting/setting.png","menuName" : "设置","type":"setGroup"}];
		}
		if(members){
			for(var i=0;i<members.length;i++){
			  if(members[i].type == 3){
				  memberList.push(members[i]);
			  }else{
				  managerList.push(members[i]);
			  }
			}
			var groupDatas = [];
			groupDatas.push({"name":"群主、管理员("+managerList.length+"人)"});
			groupDatas.push({"name":"成员("+memberList.length+"人)"});
			
			listdata4G.addData(groupDatas);
			listdata4Member.addData([managerList,memberList]);//加载成员列表
			el.bindItems(listdata4G,listdata4Member);
			el.expandGroup([0]);//展开管理员
			el.expandGroup([1]);//展开成员
		}
	});
}

//点击菜单选项
popmenuf.on("popmenuItem", function(data) {
	if(userUtil.loginToken()===""){
		nf.toast("您还没有登录哦.");
		return;
	}
	var touchIndex = parseInt(data);
	var actionType = menus[touchIndex].type;
	if(actionType=="addMember"){//邀请新成员
		if(canInviteUser){
			common.open("source://view/circle/gchat/member/invite/invite2gchat.ui",{"circleId":circleId});
		}else{
			nf.toast("您不是该群成员,不能邀请!");
		}
	}else if(actionType=="dismissGroup"){//解散群聊
		if(circleId != null){
			imHelper.sendGroupChatTip(circleId, "GroupChatTip:\""+myNick+"\" 已解散该群聊");
			circleUtil.dismissCircle(circleId, function() {
				if(imGroupId!=null){
					app.fire("fire_quit_or_dismiss_groupchat", imGroupId);//退出或解散群聊
				}
				app.closePage({dismissGroup:true});
			});
		}
	}else if(actionType=="quitGroup"){//退出群聊
		if(circleId != null){
			imHelper.sendGroupChatTip(circleId, "GroupChatTip:\""+myNick+"\" 已退出该群聊");
			circleUtil.quitCircle(circleId, function() {
				if(imGroupId!=null){
					app.fire("fire_quit_or_dismiss_groupchat", imGroupId);//退出或解散群聊
				}
				app.closePage({quitGroup:true});
			});
		}
	}else if(actionType=="setGroup"){
		common.open("source://view/circle/gchat/manage/gchatSet.ui",{"circleId":circleId});
	}else if(actionType=="manageMember"){//管理成员
		common.open("source://view/circle/gchat/member/manage/memberSet.ui",{"circleId":circleId});
	}
	popmenuf.fire("popmenuClose");
});
